package com.medical.admin.patient.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.medical.admin.patient.common.dto.PatientDto;
import com.medical.admin.patient.service.PatientService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.response.ResponseEntity;
import com.qf.medical.up.aliyun.AliOSSUtilsAutoConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * 1.查询患者信息
 * 2.添加患者信息
 * 3.修改患者信息
 * 4.删除患者
 * 5.分页查询所有状态正常的患者
 * 6.上传患者头像
 */
@Api(tags = "患者信息接口API在线文档")
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Resource
    private PatientService patientService;
    @Resource
    private AliOSSUtilsAutoConfiguration aliOSSUtilsAutoConfiguration;

    @ApiOperation("获取患者信息")
    @GetMapping("/researchPatient")
    @ApiImplicitParam(name = "patientId",value = "患者id")
    public ResponseEntity<PatientDto> researchPatient(@NotNull Long patientId) {
        return ResponseEntity.success(patientService.researchPatient(patientId));
    }

    @ApiOperation("添加患者")
    @PostMapping("/addPatient")
    @ApiImplicitParam(name = "patientDto",value = "患者Dto实体类")
    public ResponseEntity<Integer> addPatient(@RequestBody @NotNull PatientDto patientDto) {
        return ResponseEntity.success(patientService.addPatient(patientDto));
    }

    @ApiOperation("修改患者信息")
    @PutMapping("/modifyPatient")
    @ApiImplicitParam(name = "patientDto",value = "患者Dto实体类")
    public ResponseEntity<Integer> modifyPatient(@RequestBody @NotNull PatientDto patientDto) {
        return ResponseEntity.success(patientService.modifyPatient(patientDto));
    }

    @ApiOperation("删除患者")
    @DeleteMapping("/removePatient")
    @ApiImplicitParam(name = "patientId",value = "患者id")
    public ResponseEntity<Integer> removePatient(@NotNull Long patientId) {
        return ResponseEntity.success(patientService.deletePatient(patientId));
    }

    @ApiOperation("分页查询所有患者")
    @GetMapping("/findAllPatient/{page}/{size}")
    public ResponseEntity<IPage<PatientDto>> findAllPatient( @RequestParam(name = "page",defaultValue = "1")@PathVariable("page") Integer page,
                                                             @RequestParam(name = "size",defaultValue = "10")@PathVariable("size") Integer size) {
        return ResponseEntity.success(patientService.findAllPatient(page,size));
    }

    @ApiOperation("上传患者头像")
    @PostMapping("/upload")
    @ApiImplicitParam(name = "file",value = "文件上传对象")
    public String upload(@NotNull MultipartFile file){
        try {
            return aliOSSUtilsAutoConfiguration.upload(file);
        } catch (IOException e) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
    }

}
